I have some values (derived out of a formula) in a particular row in an excel sheet. I am trying to drag them down till a certain number of rows using .FillDown
method of Range
looping through all the columns.
Sub RawDataPreparation()
Dim v As Long
Dim LastRow As Long
Dim lastCol As Integer
Dim c As Integer
c = ThisWorkbook.Sheets("Metadata").Range("B10")
Dim ColName() As String
ReDim ColName(c)
Dim strFormulas() As Variant
ReDim strFormulas(c)
Dim lastColumn As Integer
With ThisWorkbook.Sheets("Raw Data")
lastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
For i = 0 To c - 1
On Error Resume Next
ColName(i) = ThisWorkbook.Sheets("Metadata").Range("C" & i + 10) 'copy the value corresponding to column C10:C18 in Metadata sheet to the array
ThisWorkbook.Sheets("Raw Data").Cells(1, lastColumn + 1 + i).Value = ColName(i)
Next i
With ThisWorkbook.Sheets("Raw Data")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
For j = 0 To c - 1
On Error Resume Next
strFormulas(j) = ThisWorkbook.Sheets("Metadata").Range("D" & j + 10) 'copy the formulas corresponding to column D10:D18 in Metadata sheet to the array
With ThisWorkbook.Sheets("Raw Data")
.Cells(2, lastColumn + 1 + j).Formula = strFormulas(j)
End With
Next j
'Code to drag down the formula till last row.
For k = 0 To c - 1
ThisWorkbook.Sheets("Raw Data").Range(Cells(2, lastColumn + 1 + k), Cells(LastRow, lastColumn + 1 + k)).FillDown
Next k
End Sub
When I execute using F8 (step by step) the last loop (to drag the formula till last row) is being executed and giving intended result. But upon executing the entire Sub RawDataPreparation
using F5, the last loop is getting omitted.
I am not able to understand this behavior. Can anyone suggest why this is happening?
You need to qualify references to Cells
, Range
, Rows
, etc unless you KNOW that your ActiveSheet
is the sheet being referred to.
So change your last statement to
ThisWorkbook.Sheets("Raw Data").Range(ThisWorkbook.Sheets("Raw Data").Cells(2, lastColumn + 1 + k), ThisWorkbook.Sheets("Raw Data").Cells(LastRow, lastColumn + 1 + k)).FillDown
Or, to make it a bit easier to read:
With ThisWorkbook.Sheets("Raw Data")
.Range(.Cells(2, lastColumn + 1 + k), .Cells(LastRow, lastColumn + 1 + k)).FillDown
End With
In order to make your code work the last loop should define the range somewhat like this:-
Dim k As Long
For k = 0 To C - 1
With ThisWorkbook.Sheets("Raw Data")
.Range(.Cells(2, lastColumn + 1 + k), .Cells(lastRow, lastColumn + 1 + k)).FillDown
End With
Next k
Observe the period before the Cells
defining the range. This period makes the references refer to the "Raw Data" sheet, whereas in your code, in the absence of any spacification, they refer to the ActiveSheet. So you get a different result not depending upon how you trigger the code but which is the active sheet at the time.
Anyway, to prove my point I had to almost rewrite your code. I continued to finish the job, and here it is.
Sub RawDataPreparation()
' 24 Mar 2017
Dim WsMetadata As Worksheet
Dim WsRawData As Worksheet
Dim firstMetaRow As Long, TransferRow As Long
Dim nextCol As Integer
Dim lastRow As Long
Dim R As Long, C As Long
firstMetaRow = 10 ' set this value here instead of in the code
Set WsMetadata = ThisWorkbook.Sheets("Metadata")
TransferRow = CLng(Val(WsMetadata.Range("B10").Value)) - 1
' B10 can't have a value < 1. In your example it should be 9
If TransferRow < 0 Then Exit Sub
Set WsRawData = ThisWorkbook.Sheets("Raw Data")
With WsRawData
nextCol = .Cells(1, .Columns.Count).End(xlToLeft).Column + 1
End With
' transpose columns C and D of WsMetaData to rows 1 and 2 in WsRawData
For R = firstMetaRow To (firstMetaRow + TransferRow)
With WsMetadata
.Cells(R, "C").Copy Destination:=WsRawData.Cells(1, (nextCol + R - firstMetaRow))
.Cells(R, "D").Copy Destination:=WsRawData.Cells(2, (nextCol + R - firstMetaRow))
End With
Next R
With WsRawData
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range(.Cells(2, nextCol), .Cells(lastRow, nextCol + TransferRow)).FillDown
End With
End Sub
It works, but unfortunately it may not do the job you envisioned. The formulas you copy from the Metadata contain references to that sheet which can't be translated 1:1 once the range is transposed. You may have to settle for pasting values or translate the formulas before filling them down.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.