VBA: Drag down cells by calculated value

noctash

New Member
Joined
Aug 4, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
I realize this is probably a very basic question, but I'm struggling to find the answer online. Thank you in advance!

I am very new to VBA and trying to write a macro to speed up my workflow, having difficulty finding a code that will drag down a variable amount of rows.

Sheet 1 with lots of information about samples - I add new samples to this one with a form
Sheet 2 has a space to enter results with replicates for each sample.

I want my macro select all the data on Sheet 2, calculate the difference between the last cell in Sheet 1 Column A and Sheet 2 Column E, and drag down by double that amount to skip rows as I have in the images below.

My idea of how the code would work:
VBA Code:
Sub Update()

'Call Value of Last Cell Sheet 1 Column A = "x"
???

'Call Value of Last Cell Sheet 2 Column E = "y"
???

'Calculate Number of cells to drag down "n" = (x - y)*2
???

'Select Cells in Sheet 2
Sheets("Sheet 2").Select
Range("E4", Range("E4").End(xlDown).End(xlToRight)).Select)
'Drag Down by "n"
???
 End Sub

1670036990235.png

1670037101889.png
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi noctash,

MrE_1223740_161520B_vba drag down cells_221204.xlsm
ABCDEFGHIJKL
1RowFirst runRow
2
3DataResult
4
53ID3ID
641Info11Info1241Info11Info12Data to be filledData to be filledData to be filled
752Info21Info225Data to be filledData to be filledData to be filled
863Info31Info3262Info21Info22Data to be filledData to be filledData to be filled
97Data to be filledData to be filledData to be filled
1083Info31Info32Data to be filledData to be filledData to be filled
119Data to be filledData to be filledData to be filled
12
13
14
15Second run
16
17DataResult
18
193ID3ID
2041Info11Info1241Info11Info12Data to be filledData to be filledData to be filled
2152Info21Info225Data to be filledData to be filledData to be filled
2263Info31Info3262Info21Info22Data to be filledData to be filledData to be filled
2374Info41Info427Data to be filledData to be filledData to be filled
2485Info51Info5283Info31Info32Data to be filledData to be filledData to be filled
259Data to be filledData to be filledData to be filled
26104Info41Info42Data to be filledData to be filledData to be filled
2711Data to be filledData to be filledData to be filled
28125Info51Info52Data to be filledData to be filledData to be filled
2913Data to be filledData to be filledData to be filled
Combined


Code in a standard module, please change the names of the worksheets to suit:

VBA Code:
Public Sub MrE_1223740_161520B()
' https://www.mrexcel.com/board/threads/vba-drag-down-cells-by-calculated-value.1223740/
Dim lngCounter      As Long
Dim lngLastSh1      As Long
Dim lngLastSh2      As Long
Dim lngStartSh2     As Long
Dim lngStartSh1     As Long
Dim wsSh1           As Worksheet
Dim wsSh2           As Worksheet

'constant to decide whether to add datra or start at beginning, here set to add
Const cblnStartOver As Boolean = False

'constant to be inserted in target sheet
Const cstrInsert As String = "Data to be filled"

'get references to the sheets
Set wsSh1 = Worksheets("Data")
Set wsSh2 = Worksheets("Result")

'get the last filled row on each sheet respectively
lngLastSh1 = wsSh1.Range("A" & wsSh1.Rows.Count).End(xlUp).Row
lngLastSh2 = wsSh2.Range("E" & wsSh2.Rows.Count).End(xlUp).Row

'check if starting with all IDs
If cblnStartOver = True Then
  lngStartSh2 = 4
  lngStartSh1 = 4
  'clear the range in Target Sheet
  With wsSh2.Range("E4")
    .Resize(.CurrentRegion.Rows.Count - 1, .CurrentRegion.Columns.Count).ClearContents
  End With
Else
  'get the information about the starting rows for each sheet
  With wsSh2.Cells(lngLastSh2, "E")
    lngStartSh2 = IIf(.Value = "ID", 4, .Row + 2)
    If .Value = "ID" Then
      lngStartSh1 = 4
    Else
      lngStartSh1 = .Value + 4
    End If
  End With
End If

With wsSh1
  'loop thorugh the data and add data to target sheet
  For lngCounter = lngStartSh1 To lngLastSh1
     wsSh2.Cells(lngStartSh2, "E").Resize(1, 3).Value = wsSh1.Cells(lngCounter, "A").Resize(1, 3).Value
     wsSh2.Cells(lngStartSh2, "E").Offset(, 3).Resize(2, 3).Value = cstrInsert
     lngStartSh2 = lngStartSh2 + 2
  Next lngCounter
End With

Set wsSh2 = Nothing
Set wsSh1 = Nothing
End Sub

Ciao,
Holger
 
Upvote 0
Solution

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top