VBA and looping

wigarth

Board Regular
Joined
Apr 16, 2016
Messages
51
Office Version
  1. 365
Platform
  1. Windows
Hi!

I have little experience with loop functions, so I am looking for a bit of help.

I have provided a 2 part code that works fine, but the problem is that for each part of the code, I will need to change some row numbers.
I was simply wondering if i could loop the code somehow with defining a row number that increases for each run in the loop? If not i will need to make 45 parts of the same code...

The code will need to loop from the value 6 to 58
And of course, to make it complicated... The row numbers: 21-24 and 40-43 are to be skipped...

I am sure this can be done somehow, but i am not skilled enough yet to do so.
Can someone please help?

Best reggards:
Wigarth

VBA Code:
'Code in 45 parts!!!... (Each "PART" has a designated row-number spanning from "6" to "58" in the columns "S" and "T"
'BUT: IMPORTANT!!! ROW/COLUMN (S21:T24) & (S40:T43) are supposed to be skipped somehow

'part 1 of code

AREA1_TRIP1: 'rownumber here is "6"
If ThisWorkbook.Sheets("Epost").Range("f6").Value = "" Then ' "F".value -changes in next part
GoTo AREA1_TRIP2
Else
End If

'"T".value below changes in next part
beskjed.beskjedtekst.Caption = " - Creating data for: " & ThisWorkbook.Sheets("Epost").Range("t6").Value
.Repaint

'Both "S".values below changes in next part
If ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S6").Value).Range("y2").Value = 1 Then
ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S6").Value).Range("A1:K34").ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Sheets("Feilretting").Range("c20").Value & "Tur1-Rute1.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
GoTo AREA1_TRIP2
Else
End If

'Both "S".values below changes in next part
If ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S6").Value).Range("y2").Value = 2 Then
ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S6").Value).Range("A1:K70").ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Sheets("Feilretting").Range("c20").Value & "Tur1-Rute1.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
GoTo AREA1_TRIP2
Else
End If

'Both "S".values below changes in next part
If ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S6").Value).Range("y2").Value = 3 Then
ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S6").Value).Range("A1:K105").ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Sheets("Feilretting").Range("c20").Value & "Tur1-Rute1.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
GoTo AREA1_TRIP2
Else
End If

'The "S".value below changes in next part
ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S6").Value).Range("A1:K140").ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Sheets("Feilretting").Range("c20").Value & "Tur1-Rute1.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
'End of part 1


'PART 2 of code

AREA1_TRIP2: 'rownumber here is "7"
If ThisWorkbook.Sheets("Epost").Range("f7").Value = "" Then ' "F".value -changes in next part
GoTo AREA1_TRIP3
Else
End If

'"T".value below changes in next part
beskjed.beskjedtekst.Caption = " - Creating data for: " & ThisWorkbook.Sheets("Epost").Range("t7").Value
.Repaint

'Both "S".values below changes in next part
If ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S7").Value).Range("y2").Value = 1 Then
ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S7").Value).Range("A1:K34").ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Sheets("Feilretting").Range("c20").Value & "Tur1-Rute2.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
GoTo AREA1_TRIP3
Else
End If

'Both "S".values below changes in next part
If ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S7").Value).Range("y2").Value = 2 Then
ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S7").Value).Range("A1:K70").ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Sheets("Feilretting").Range("c20").Value & "Tur1-Rute2.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
GoTo AREA1_TRIP3
Else
End If

'Both "S".values below changes in next part
If ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S7").Value).Range("y2").Value = 3 Then
ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S7").Value).Range("A1:K105").ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Sheets("Feilretting").Range("c20").Value & "Tur1-Rute2.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
GoTo AREA1_TRIP3
Else
End If

'The "S".value below changes in next part
ThisWorkbook.Sheets(ThisWorkbook.Sheets("Epost").Range("S7").Value).Range("A1:K140").ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Sheets("Feilretting").Range("c20").Value & "Tur1-Rute2.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
'End of part 2
 
Last edited:
First of all Thanks for all the help. I am learning new things here.

It still is not working, i feel somehow that there is some minor detail just spelled wrong or something now.

Code fails right after "IF ROWEND <>0"
And the editor marks upp all text after this and to all the "End if" in the bottom
trying to read the last part of code, but it is a bit confusing atm. To many variables
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
SOLVED!

Turns out that the whole time the fact that the "sheets" was defined as "hidden" caused it to fail.
But now it works like a charm.

Thank you all so much for all the help.
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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