Skipping blank cells using =OFFSET and a macro that decides how many rows to offset

gaberen

New Member
Joined
Apr 5, 2022
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hi! I'm trying to create a macro that'll help me change how many rows to offset based on if it detects a blank cell or not.
First I'll show you what I'm trying to get at.

Book1.xlsm
ABCDEFGHIJKLMNOP
1DataIntended ListFirst AttemptSecond Attempt w/out VBA
2NoDrink ComboFruitsScoreNoDrink ComboScoreNoDrink ComboScoreNoDrink ComboScore
31ABanana1A501A01A0
4Grape502B802B02B0
53C603C603C60
62BBanana4D104D04D0
7Grape
8Apple80Intended Second Attempt w/ VBA Script
9NoDrink ComboScore
103CStrawberry601A50
112B80
124DBlueberry3C60
13Raspberry104D10
Sheet1
Cell Formulas
RangeFormula
K3:K6K3=VLOOKUP($J3, A:B, 2, FALSE)
L3:L6L3=VLOOKUP($J3, A:D, 4, FALSE)
O3:O6O3=VLOOKUP($J3, A:B, 2, FALSE)
P12,P3:P6P3=OFFSET(INDEX(D:D, MATCH(N3, A:A, 0)), 0, 0)
O10:O13O10=VLOOKUP($J3, A:B, 2, FALSE)
P10,P13P10=OFFSET(INDEX(D:D, MATCH(N10, A:A, 0)), 1, 0)
P11P11=OFFSET(INDEX(D:D, MATCH(N11, A:A, 0)), 2, 0)


And then here's my vba code that I'm having trouble with.
NOTE: I selected a cell with a "0" in it first before starting the macro
VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveCell.Select
      
    Dim OffsetNum As Integer
    OffsetNum = 0
    
    Do While ActiveCell = "0"
        OffsetNum = OffsetNum + 1
    Loop
    
    ActiveCell.Value = "=OFFSET(INDEX(D:D; MATCH(N3; A:A; 0)); " & OffsetNum & "; 0)"

End Sub

My first problem is that I get the error "overflow" from
VBA Code:
Do While ActiveCell = "0"
        OffsetNum = OffsetNum + 1
    Loop
The order I'm expecting the code will go is:
Code detects that the ActiveCell is 0 -> OffsetNum (number of rows to be offset downwards) is increased by 1 -> if ActiveCell is still 0 then -> OffsetNum is increased by 1 again -> repeat until ActiveCell is no longer 0

My second problem is that I get the error "application-defined or object-defined error" from
VBA Code:
    ActiveCell.Value = "=OFFSET(INDEX(D:D; MATCH(N3; A:A; 0)); " & OffsetNum & "; 0)"
It seems like the error is because the cell contains a string of more than 911 characters? I thought that it might be because my OffsetNum loop never actually stopped. But then I tried removing that loop and I still got the same error.

Any help and/or if anyone could help point me to the right way and what to learn I'd super appreciate it. Thanks for reading all the way here!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Solution with formula:
Book1
ABCDEFGH
1DataIntended List
2NoDrink ComboFruitsScoreNoDrink ComboScore
31ABanana1A50
4Grape502B80
53C60
62BBanana4D10
7Grape   
8Apple80   
9   
103CStrawberry60   
11   
124DBlueberry   
13Raspberry10   
Sheet1
Cell Formulas
RangeFormula
F3:F13F3=IF(MAX($F$2:F2)+1<=MAX($A:$A),MAX($F$2:F2)+1,"")
G3:G13G3=IF(F3="","",VLOOKUP(F3,$A$3:$D$13,2,0))
H3:H13H3=IF(F3="","",IFERROR(SUM(INDEX(INDEX(D:D,MATCH(F3+1,A:A,0)-1):$D$3,)),SUM(D:D))-SUM($H$2:H2))
 
Upvote 0
Solution
Solution with formula:
Book1
ABCDEFGH
1DataIntended List
2NoDrink ComboFruitsScoreNoDrink ComboScore
31ABanana1A50
4Grape502B80
53C60
62BBanana4D10
7Grape   
8Apple80   
9   
103CStrawberry60   
11   
124DBlueberry   
13Raspberry10   
Sheet1
Cell Formulas
RangeFormula
F3:F13F3=IF(MAX($F$2:F2)+1<=MAX($A:$A),MAX($F$2:F2)+1,"")
G3:G13G3=IF(F3="","",VLOOKUP(F3,$A$3:$D$13,2,0))
H3:H13H3=IF(F3="","",IFERROR(SUM(INDEX(INDEX(D:D,MATCH(F3+1,A:A,0)-1):$D$3,)),SUM(D:D))-SUM($H$2:H2))
oh my god I don't know what these formulas mean yet but I'm amazed they really work!! Thank you so much!!! I'll try to learn the functions you used here.
 
Upvote 0

Forum statistics

Threads
1,215,584
Messages
6,125,674
Members
449,248
Latest member
wayneho98

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