VBA Noob. All but the last step works... GAH!!

MustacheKaboom

New Member
Joined
Dec 20, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I'm teaching myself VBA and have run up against a wall. I've been getting help from MS Bing COPILOT with moderate success until now.

I have a daily report that I export from Power BI (into a .XLSX) and open with EXCEL. The columns always span A:M and are always of the same format but the number of rows can vary from 1 to 75.
The script does a number of steps to make the report ready to be used in my daily processes. However, the last step is not doing what I want. I need the last step to select any rows that contain data and cut them all to the clipboard. I will manually paste them into a separate workbook. The last step is currently only selecting and cutting active rows from column "E".

A sample from the report (starts at A1):
countrefill_due_dateDate refilledNotesrefills_remainingpatient_idfirst_namelast_namepatient_profile_urlclinic_name
1​
2023-12-20 15:16:21.103479
1​
124500​
Patientonehttps://pt_1_url_as_unclickable_text.com/Clinic 1
1​
2023-12-20 15:18:34.480758
1​
224583​
Patienttwohttps://pt_2_url_as_unclickable_text.com/
Clinic 2
1​
2023-12-20 15:18:45.84492
3​
324763​
Patientthreehttps://pt_3_url_as_unclickable_text.com/Clinic 3
1​
2023-12-20 15:18:56.138631
3​
424764​
Patientfourhttps://pt_4_url_as_unclickable_text.com/Clinic 4
1​
2023-12-20 15:20:33.758495
3​
524768​
Patientfivehttps://pt_5_url_as_unclickable_text.com/Clinic 5

On the report in question, my columns are formatted thusly: A is "date", B&C are left blank intentionally, columns D&E are "number" and columns F:M are "general".

The Script:
VBA Code:
Sub test_4()
'
' test_4 Macro
'
    ' Preps sheet for cut/paste by deleting extranious row/ column
    Rows("1:1").Select
    Selection.Delete Shift:=xlUp
    Columns("A:A").Select
    Selection.Delete Shift:=xlToLeft
    
    ' Loop through each cell in the range
        ' A1:A75 replaces Power BI date/ time mess with mm/dd/yyyy
            'BUT is "today" of when macro run. Won't just change format of the "date" as exported from BI.
            'BI only exports as text format, EXCEL won't recognize it as a date.
        For Each cell In Range("A1:A75")
        ' Check if the cell is not empty
        If cell.Value <> "" Then
            ' Insert the date
            cell.Value = Date
        End If
    Next cell
    
        ' D1:D75 colors refills remaining
    Range("D1:D75").Select
   
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=1"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    
    Selection.FormatConditions(1).StopIfTrue = False
        ' E1:E75 colors duplicate PT ITs
    Range("E1:E75").Select
    Selection.FormatConditions.AddUniqueValues
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).DupeUnique = xlDuplicate
    With Selection.FormatConditions(1).Font
        .Color = -16752384
        .TintAndShade = 0
    End With
    
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13561798
        .TintAndShade = 0
    
   End With
    
   Selection.FormatConditions(1).StopIfTrue = False
    
    'Loop through each cell in the range H1:H75 makes URLs clickable
    For Each cell In Range("H1:H75")
        ' Check if the cell is not empty
        If cell.Value <> "" Then
            ' Convert the text URL into a clickable hyperlink
            ActiveSheet.Hyperlinks.Add Anchor:=cell, Address:=cell.Value
        End If
    Next cell
    
        ' Below will eventually select/ cut entire body to clipboard... hopefully.
       ' Select and cut only the cells that have data
    On Error Resume Next ' In case there are no constants
    Range("A1:M75").SpecialCells(xlCellTypeConstants).Select
    Selection.Cut
    On Error GoTo 0 ' Reset error handling
End Sub

Everything works as desired except:

VBA Code:
   On Error Resume Next ' In case there are no constants
    Range("A1:M75").SpecialCells(xlCellTypeConstants).Select
    Selection.Cut
    On Error GoTo 0 ' Reset error handling

I have tried SpecialCells(xlCellTypeConstants), (xlCellTypeConstants + xlCellTypeFormulas), and (xlCellTypeVisible). But I'm gathering that I don't actually understand what my spreadsheet contains if the script is only finding constants on one "number" column and no formulas. Is there something like SpecialCells that just recognizes that cells contain "something" but doesn't care what?
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I'm teaching myself VBA and have run up against a wall. I've been getting help from MS Bing COPILOT with moderate success until now.

I have a daily report that I export from Power BI (into a .XLSX) and open with EXCEL. The columns always span A:M and are always of the same format but the number of rows can vary from 1 to 75.
The script does a number of steps to make the report ready to be used in my daily processes. However, the last step is not doing what I want. I need the last step to select any rows that contain data and cut them all to the clipboard. I will manually paste them into a separate workbook. The last step is currently only selecting and cutting active rows from column "E".

A sample from the report (starts at A1):
countrefill_due_dateDate refilledNotesrefills_remainingpatient_idfirst_namelast_namepatient_profile_urlclinic_name
1​
2023-12-20 15:16:21.103479
1​
124500​
Patientonehttps://pt_1_url_as_unclickable_text.com/Clinic 1
1​
2023-12-20 15:18:34.480758
1​
224583​
Patienttwohttps://pt_2_url_as_unclickable_text.com/
Clinic 2
1​
2023-12-20 15:18:45.84492
3​
324763​
Patientthreehttps://pt_3_url_as_unclickable_text.com/Clinic 3
1​
2023-12-20 15:18:56.138631
3​
424764​
Patientfourhttps://pt_4_url_as_unclickable_text.com/Clinic 4
1​
2023-12-20 15:20:33.758495
3​
524768​
Patientfivehttps://pt_5_url_as_unclickable_text.com/Clinic 5

On the report in question, my columns are formatted thusly: A is "date", B&C are left blank intentionally, columns D&E are "number" and columns F:M are "general".

The Script:
VBA Code:
Sub test_4()
'
' test_4 Macro
'
    ' Preps sheet for cut/paste by deleting extranious row/ column
    Rows("1:1").Select
    Selection.Delete Shift:=xlUp
    Columns("A:A").Select
    Selection.Delete Shift:=xlToLeft
   
    ' Loop through each cell in the range
        ' A1:A75 replaces Power BI date/ time mess with mm/dd/yyyy
            'BUT is "today" of when macro run. Won't just change format of the "date" as exported from BI.
            'BI only exports as text format, EXCEL won't recognize it as a date.
        For Each cell In Range("A1:A75")
        ' Check if the cell is not empty
        If cell.Value <> "" Then
            ' Insert the date
            cell.Value = Date
        End If
    Next cell
   
        ' D1:D75 colors refills remaining
    Range("D1:D75").Select
  
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=1"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
   
    Selection.FormatConditions(1).StopIfTrue = False
        ' E1:E75 colors duplicate PT ITs
    Range("E1:E75").Select
    Selection.FormatConditions.AddUniqueValues
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).DupeUnique = xlDuplicate
    With Selection.FormatConditions(1).Font
        .Color = -16752384
        .TintAndShade = 0
    End With
   
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13561798
        .TintAndShade = 0
   
   End With
   
   Selection.FormatConditions(1).StopIfTrue = False
   
    'Loop through each cell in the range H1:H75 makes URLs clickable
    For Each cell In Range("H1:H75")
        ' Check if the cell is not empty
        If cell.Value <> "" Then
            ' Convert the text URL into a clickable hyperlink
            ActiveSheet.Hyperlinks.Add Anchor:=cell, Address:=cell.Value
        End If
    Next cell
   
        ' Below will eventually select/ cut entire body to clipboard... hopefully.
       ' Select and cut only the cells that have data
    On Error Resume Next ' In case there are no constants
    Range("A1:M75").SpecialCells(xlCellTypeConstants).Select
    Selection.Cut
    On Error GoTo 0 ' Reset error handling
End Sub

Everything works as desired except:

VBA Code:
   On Error Resume Next ' In case there are no constants
    Range("A1:M75").SpecialCells(xlCellTypeConstants).Select
    Selection.Cut
    On Error GoTo 0 ' Reset error handling

I have tried SpecialCells(xlCellTypeConstants), (xlCellTypeConstants + xlCellTypeFormulas), and (xlCellTypeVisible). But I'm gathering that I don't actually understand what my spreadsheet contains if the script is only finding constants on one "number" column and no formulas. Is there something like SpecialCells that just recognizes that cells contain "something" but doesn't care what?

EDIT: so, without me making any changes, the script suddenly changed behaviors. It is now selecting all cells with data (good!) but can't do the cut because columns B&C are blank, making it multiple ranges, thus can't be cut (Booo! Bad.)

Is there a way to make a cell location into a variable then just do a range (A1:%var_cell%) then select the custom range and cut it?
 
Upvote 0
Final update: I finally asked Copilot the right question. The solution is to use:
VBA Code:
' Find the last row with data in column A
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    ' Select and cut the entire range
    Range("A1:M" & lastRow).Select
    Selection.Cut
This selects all the active cells along with the blank cells, then cuts them to the clipboard. Exactly what I needed.
 
Upvote 0
Solution
Note: Just a little tip. You can get rid of most of the "Select" statements. It is usually not necessary to select ranges before working with them (a lot of this comes from the Macro Recorder, which is very literal). You can usually combine the lines where one ends in ".Select" and the next starts with "Selection.". Doing so will not only shorten your code, it will also make it faster.

So lines like this:
VBA Code:
    Rows("1:1").Select
    Selection.Delete Shift:=xlUp
could be combined/simplified to this:
VBA Code:
    Rows("1:1").Delete Shift:=xlUp

as well as lines like this:
VBA Code:
    Range("A1:M" & lastRow).Select
    Selection.Cut
can be simplified to this:
VBA Code:
    Range("A1:M" & lastRow).Cut
 
Upvote 0
Note: Just a little tip. You can get rid of most of the "Select" statements. It is usually not necessary to select ranges before working with them (a lot of this comes from the Macro Recorder, which is very literal). You can usually combine the lines where one ends in ".Select" and the next starts with "Selection.". Doing so will not only shorten your code, it will also make it faster.

So lines like this:
VBA Code:
    Rows("1:1").Select
    Selection.Delete Shift:=xlUp
could be combined/simplified to this:
VBA Code:
    Rows("1:1").Delete Shift:=xlUp

as well as lines like this:
VBA Code:
    Range("A1:M" & lastRow).Select
    Selection.Cut
can be simplified to this:
VBA Code:
    Range("A1:M" & lastRow).Cut
Ohhhh.... that's slick! I'll give it a try next time I'm under the hood. Thanks!!!!
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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