抵消数据的动态范围
Offsetting a dynamic range of data
我遇到了一些看似非常基本的问题。我对 vba 的体验基本上没什么。我有一个电子表格,其中包含使用我制作的脚本从一些 csv 文件导入的数据。该脚本读入数据,然后创建一个主电子表格或覆盖以前的主电子表格。我想在同一个脚本中将此数据移动一列。随着更多数据的添加,每次更新电子表格时,需要移动的数据范围都会发生变化。我在我的代码末尾包含了以下内容来执行此操作,但我无法让它工作。
Set myRange = Range(Cells(1, 1).Address(), Cells(lastRow, lastColumn).Address())
Range("myRange").Offset(,1)
我做错了什么?
编辑:这是我现在拥有的。我在第 14 行将 运行 保留为 'Run-time error 1004'。
Dim myRange As Range
Dim DefPath As String
Dim lastRow As Long
Dim lastColumn As Long
Dim lRow As Long
Dim lCol As Long
DefPath = Application.DefaultFilePath
Workbooks.Open Filename:=DefPath & "\SEM Master File"
lRow = Cells(Rows.Count, 1).End(xlUp).Row
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
Set myRange = Range(Cells(1, 1), Cells(lastRow, lastColumn))
myRange.Offset(, lastColumn).Value = myRange.Value
第二次编辑:这是我目前所拥有的。它会编译并运行,但它并没有像我预期的那样将所有内容移动到一列上,而是复制了这些列并将其放在电子表格中已有数据旁边的列中。
Sub Offset_Data()
Dim myRange As Range
Dim DefPath As String
Dim lastRow As Long
Dim lastColumn As Long
DefPath = Application.DefaultFilePath
Workbooks.Open ("SEM Master File.xlsx")
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
Set myRange = Range(Cells(1, 1), Cells(lastRow, lastColumn))
myRange.Offset(, lastColumn).Value = myRange.Value
End Sub
Sub Offset_Data()
Dim myRange As Range
Dim DefPath As String
Dim lastRow As Long
Dim lastColumn As Long
DefPath = Application.DefaultFilePath
Workbooks.Open ("SEM Master File.xlsx")
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
Set myRange = Range(Cells(1, 1), Cells(lastRow, lastColumn))
myRange.Offset(, lastColumn).Value = myRange.Value
myRange.ClearContents
End Sub
我遇到了一些看似非常基本的问题。我对 vba 的体验基本上没什么。我有一个电子表格,其中包含使用我制作的脚本从一些 csv 文件导入的数据。该脚本读入数据,然后创建一个主电子表格或覆盖以前的主电子表格。我想在同一个脚本中将此数据移动一列。随着更多数据的添加,每次更新电子表格时,需要移动的数据范围都会发生变化。我在我的代码末尾包含了以下内容来执行此操作,但我无法让它工作。
Set myRange = Range(Cells(1, 1).Address(), Cells(lastRow, lastColumn).Address())
Range("myRange").Offset(,1)
我做错了什么?
编辑:这是我现在拥有的。我在第 14 行将 运行 保留为 'Run-time error 1004'。
Dim myRange As Range
Dim DefPath As String
Dim lastRow As Long
Dim lastColumn As Long
Dim lRow As Long
Dim lCol As Long
DefPath = Application.DefaultFilePath
Workbooks.Open Filename:=DefPath & "\SEM Master File"
lRow = Cells(Rows.Count, 1).End(xlUp).Row
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
Set myRange = Range(Cells(1, 1), Cells(lastRow, lastColumn))
myRange.Offset(, lastColumn).Value = myRange.Value
第二次编辑:这是我目前所拥有的。它会编译并运行,但它并没有像我预期的那样将所有内容移动到一列上,而是复制了这些列并将其放在电子表格中已有数据旁边的列中。
Sub Offset_Data()
Dim myRange As Range
Dim DefPath As String
Dim lastRow As Long
Dim lastColumn As Long
DefPath = Application.DefaultFilePath
Workbooks.Open ("SEM Master File.xlsx")
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
Set myRange = Range(Cells(1, 1), Cells(lastRow, lastColumn))
myRange.Offset(, lastColumn).Value = myRange.Value
End Sub
Sub Offset_Data()
Dim myRange As Range
Dim DefPath As String
Dim lastRow As Long
Dim lastColumn As Long
DefPath = Application.DefaultFilePath
Workbooks.Open ("SEM Master File.xlsx")
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
Set myRange = Range(Cells(1, 1), Cells(lastRow, lastColumn))
myRange.Offset(, lastColumn).Value = myRange.Value
myRange.ClearContents
End Sub