我有多列,请问我如何提供范围 帮助我,我将字符串添加到mysql数据库
aWidth = WorksheetFunction.CountA(Range("A1:FA1")) 'Finds the width of the table - if you have more columns than that, just extend the range
aHeight = WorksheetFunction.CountA(Range("A1:A65536")) - 1 'Finds the height of the table, minus the field names - if you have more rows than that, just extend the range
count = 0 'Will be used throughout the macro as a counter
count_2 = 0 'Will be used throughout the macro as a counter
count_3 = 0 'Will be used throughout the macro as a counter
'------------------------------------------------------------------------------
'Populate the table row
'------------------------------------------------------------------------------
ReDim array_fields(aWidth)
'This will populate INTO what the VALUES will go for the whole upload
Do Until count = aWidth
count = count + 1 'Set the count to be used in the array and increment it for the the
Do
cell = Worksheets("Upload").Cells(1, count).Value
array_fields(count) = cell
Loop
答案 0 :(得分:0)
我不清楚你在尝试什么,但我认为下面的宏将帮助你找到正确的方向。
创建一个新工作簿,打开VBA编辑器,创建一个模块并将下面的宏复制到模块中。
将="R"&ROW(A1)&"-C"&COLUMN(A1)
复制到工作表Sheet1的单元格A1。这个公式不是必不可少的,但会显示我希望非常清楚地证明的效果。
复制单元格A1,然后向下复制第1行以创建一个小矩形(比如5 * 10)的值。
运行宏。
语句cellValue = .UsedRange.Value
导入整个使用范围的工作表" Sheet1"作为数组并将其存储在变体cellValue
。
对于二维数组,惯例是将列作为第一维,将行作为第二维。通过加载范围填充的数组是相反的。
语句cellValue = WorksheetFunction.Transpose(.UsedRange.Value)
也会导入范围,但会交换行和列。
如果在将1-D数组写入数据库之前必须将一行值复制到一维数组,则转置将没什么价值。但是,如果您可以编写二维数组,则可能会将数据转换为单次写入所需的格式。
Option Explicit
Sub Demo()
Dim cellValue As Variant
Dim ColCrnt As Long
Dim RowCrnt As Long
With Worksheets("Sheet1")
cellValue = .UsedRange.Value
For RowCrnt = 1 To UBound(cellValue, 2)
For ColCrnt = 1 To UBound(cellValue, 1)
Debug.Print "|" & cellValue(ColCrnt, RowCrnt);
Next
Debug.Print "|"
Next
Debug.Print ("-----------------------------------------")
cellValue = WorksheetFunction.Transpose(.UsedRange.Value)
For RowCrnt = 1 To UBound(cellValue, 2)
For ColCrnt = 1 To UBound(cellValue, 1)
Debug.Print "|" & cellValue(ColCrnt, RowCrnt);
Next
Debug.Print "|"
Next
End With
End Sub