Getting a macro that when it detects a blank cell to run 2 other macros and then end.

bracken752

New Member
Joined
Aug 15, 2013
Messages
44
Hey all,

I have a macro that when the sheet opens, it will automatically go to a website and pull 20-30 (This number can change each time, it depends on how much data have been put on in any given week) tables worth of data.

I have this Macro working however at the end of the macro I want it to remove some of the sheets that are no longer need (The data sheet used to pull the data in the first place and a 'Welcome' Sheet) again, I have a working macro for this (but cant get it to worth in one macro) and after this macro to save the file and publish it.

The macros are as follows:

Macro to go and fetch all of the data, using concatenated URLs to go and fetch the data from different cells D28:D63 (but this can change depending on factors) so I have a If blank cell on there)

Code:
Private Sub Workbook_Open()

For Each Cell In Range("InputSheet!D28:D100")
If IsEmpty(Cell) Then Exit For


Application.ScreenUpdating = False
ActiveWorkbook.Worksheets.Add
Selection.Copy
Application.CutCopyMode = False
Range("D" & Rows.Count).End(xlUp).Offset(1).Select
 With ActiveSheet.QueryTables.Add(Connection:="URL;" & Cell.Value, Destination:=Range("A1"))
.Name = "names"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
 End With
    ActiveSheet.Name = Range("B10").Value & " " & Range("C10").Value
    removeheaders
      
Next Cell


Clearupend
Save


End Sub

Macro to clear up the now useless sheets (only needed to get the information in the first place).

Code:
Sub Clearupend()

Application.DisplayAlerts = False
Worksheets("InputSheet").Delete
Worksheets("Data").Delete
Application.DisplayAlerts = True


End Sub

Macro to Save and Publish the file to a location on one of our servers to be viewed as html file (or as .xlm if they prefer hence the save and publish).

Code:
Sub Save()ActiveWorkbook.SaveCopyAs Filename:="DIRECTORYREMOVED" & ActiveWorkbook.Name


With ActiveWorkbook.PublishObjects.Add(xlSourceWorkbook, _
        "DIRECTORYREMOVED\Workbookname.htm", , , xlHtmlStatic, _
        "Workbookname_15399", "")
        .Publish (True)
        .AutoRepublish = False
    End With
    ChDir "DIRECTORYREMOVED"
End Sub

Any suggestions would be great <3 you guys in advanced.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
This probably won't make a difference but you could try it (See my last note below about this name if you want to try this method):

Code:
Application.Run "Clearupend"
Applicatin.Run "Save"

Perhaps a more straight forward approach would be to put all the code in one sub:

Code:
[Private Sub Workbook_Open()

For Each Cell In Range("InputSheet!D28:D100")
If IsEmpty(Cell) Then Exit For


Application.ScreenUpdating = False
ActiveWorkbook.Worksheets.Add
Selection.Copy
Application.CutCopyMode = False
Range("D" & Rows.Count).End(xlUp).Offset(1).Select
 With ActiveSheet.QueryTables.Add(Connection:="URL;" & Cell.Value, Destination:=Range("A1"))
.Name = "names"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
 End With
    ActiveSheet.Name = Range("B10").Value & " " & Range("C10").Value
    removeheaders
      
Next Cell


Application.DisplayAlerts = False
Worksheets("InputSheet").Delete
Worksheets("Data").Delete
Application.DisplayAlerts = True

With ActiveWorkbook.PublishObjects.Add(xlSourceWorkbook, _
        "DIRECTORYREMOVED\Workbookname.htm", , , xlHtmlStatic, _
        "Workbookname_15399", "")
        .Publish (True)
        .AutoRepublish = False
End With
ChDir "DIRECTORYREMOVED"


End Sub

Personally, I avoid using a name like Save for a sub or Cell for a variable. "SaveSub", "SavePublish", "rCell", "Cell1", etc. or something else to alter the word slightly could help to avoid confusion. Take that with a grain of salt.
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,693
Members
449,117
Latest member
Aaagu

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