Excel 2011 vba:如何循环列的行,并乘以一个单一的值的所有行?

我只想得到2列(想象他们将主持像x和y坐标的数字值),并从列(所有)(A)(对于x)除列C中的某个值的所有值(又名行)。而且我想在列D中为B列(对于Y)做同样的事情。

这是我得到了多less。

(差不多忘了告诉大家,比例高度和比例宽度是通过H4和I4(800×600)H3和I3(例如1024×769)的分割得到的,H3 / I3(存在比例宽度)和H4 / I4(它存储在proportion_height)我只需要知道如何将列A到C和列B到D这两个值相乘。就是这样!

Sub landmarks_resizer() ' Creating variables to store the proportion of the new map. Whatever (size) it is. Dim proportion_width As Long Dim proportion_height As Long Dim size_of_column As Long Dim current_row As Long ' Just checking for NON zero values to avoid errors... If H3 > 0 And H4 > 0 And I3 > 0 And I4 > 0 Then proportion_width = H3 / H4 proportion_height = I3 / I4 End If ' Changing headers of these columns to better identify them with new values Range("C1") = "Resized X" Range("D1") = "Resized Y" ' Go to the very last row of column A. And from there goes Up. Which will go to the last row of column A. :-) Range("A" & Rows.Count).End(xlUp).Select current_row = ActiveCell.Row With Range("H1") '<--| reference a "helper" free cell (change "H1" to your needs) .Value = proportion_width '<--| store the dividing factor in the "helper" cell .Copy '<--| store the dividing factor in clipboard End With With Range("A1", Cells(Rows.Count, 1).End(xlUp)) .Offset(, 2).Value = .Value '<--| copy column A values to columns C .Offset(, 2).PasteSpecial Operation:=xlPasteSpecialOperationDivide '<--| divide column C values by the value in clipboard End With Range("H1").ClearContents '<--| clear the content of the "helper" cell Application.CutCopyMode = False '<--| release the clipboard End Sub 

我在这里添加了我的同事的代码,我几乎到了那里! 每当我运行这个macros,它就会说'#DIV / 0!'。

你可以利用Range对象的PasteSpecial()方法

这是通过proportion_width宽度将列A值分开并将结果放在列C中的示例

 With Range("H1") '<--| reference a "helper" free cell (change "H1" to your needs) .Value = proportion_width '<--| store the dividing factor in the "helper" cell .Copy '<--| store the dividing factor in clipboard End With With Range("A1", Cells(Rows.Count, 1).End(xlUp)) .Offset(, 2).Value = .Value '<--| copy column A values to columns C .Offset(, 2).PasteSpecial Operation:=xlPasteSpecialOperationDivide '<--| divide column C values by the value in clipboard End With Range("H1").ClearContents '<--| clear the content of the "helper" cell Application.CutCopyMode = False '<--| release the clipboard 

你可以采取类似的行列B到D