VBA-Copying a column from one sheet to another conditionally

FrostyBostie

New Member
Joined
Feb 2, 2020
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
So I want to cycle through all the sheets, Check cell E1, and if it has "flex" within the string, it copies column B and appends it to the next column on the first page "Mastersheet".. not working for some reason, Seems fairly straight forward but i'm probably missing something. pretty new to this. Oh not sure if it makes a difference but A1-D1 and E1-N1 are 2 large merged cells



Thanks for any help.


This is cross-posted Here

VBA Code:
Function fn_LastColumn

 Dim lastCol As Long
 lastCol = Sht.Cells.SpecialCells(xlLastCell).Column
 lCol = Sht.Cells.SpecialCells(xlLastCell).Column
 Do While Application.CountA(Sht.Columns(lCol)) = 0 And lCol <> 1
 lCol = lCol - 1
 Loop
 fn_LastColumn = lCol

End Function

On Error GoTo IfError

Dim Sht As Worksheet, DstSht As Worksheet
Dim LstRow As Long, LstCol As Long, DstCol As Long
Dim var As String

With Application
 .ScreenUpdating = False
 .EnableEvents = False
End With
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Sheets("Mastersheet").Delete
Application.DisplayAlerts = True

Sheets.Add(Before:=Sheets(1)).Name = "Mastersheet"

For Each Sht In ActiveWorkbook.Worksheets
 DstCol = fn_LastColumn(Mastersheet)
 
 If DstCol = 1 Then DstCol = 0
 
 var = Cells(1, 5).Value
 If InStr(var, "flex") Then
 Columns(2).Copy Sheets("Mastersheet").Cells(1, DstCol + 1)
 End If

 Next
 
IfError:
 With Application
 .ScreenUpdating = True
 .EnableEvents = True
 End With

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
How about
VBA Code:
Sub FrostyBostie()
   Dim Ws As Worksheet, Mws As Worksheet
   Dim NxtCol As Long
   
   Application.ScreenUpdating = False
   If Evaluate("Isref(Mastersheet!A1)") Then
      Sheets("Mastersheet").Cells.Clear
   Else
      Sheets.Add(Sheets(1)).Name = "Mastersheet"
   End If
   Set Mws = Sheets("Mastersheet")
   For Each Ws In Worksheets
      If InStr(1, Ws.Range("E1"), "flex", vbTextCompare) > 0 Then
         NxtCol = NxtCol + 1
         Ws.Range("B:B").Copy Mws.Cells(1, NxtCol)
      End If
   Next Ws
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,659
Messages
6,126,077
Members
449,286
Latest member
Lantern

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