Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I would like to learn how to copy cells from an already open excel workbook to another file. I don't just want a code, I want a tutorial. The problem I have is Google is only giving me forums. Can someone suggest a good tutorial with a video? A hyper link would help.
Option Explicit
Sub CopyFromOpenWorkbookToAnother()
'declare variables
Dim wkbCopy as Workbook, wkbPaste as Workbook
Dim wksCopy as Worksheet, wksPaste as Worksheet
Dim rngCopy as Range
'set variables -> change the names to suit your data needs
Set wkbCopy = Workbooks("WorkbookToCopyFrom.xls")
Set wkbPaste = Workbooks("WorkbookToCopyTo.xls")
Set wksCopy = wkbCopy.Sheets("SheetToCopy")
Set wksPaste = wkbPaste.Sheets("SheetToPaste")
Set rngCopy = wksCopy.Range("A1:A10") 'adjust to whatever range you need
rngCopy.Copy wksPaste.Range("A1") 'adjust to wherever you want to paste
End Sub
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have to run a loop in multiple sheets by getting the last column for each sheet. How can I get the last column of every sheet? I tried putting this a function. Can you please suggest on this as I am just a beginner on this.
Update:
Its working fine now. Tried the code provided by #sktneer. Thanks everyone for your inputs.
You may try something like this...
Sub LastColumnInEachSheet()
Dim ws As Worksheet
Dim lc As Long
For Each ws In ThisWorkbook.Sheets
lc = ws.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Debug.Print ws.Name, lc
Next ws
End Sub
Below code will go through all worksheets in the workbook:
Dim oWS As Worksheet
For Each oWS In ThisWorkbook.Worksheets
oWS.Name ' Give you the sheet name
oWS.Cells(1, oWS.Columns.count).End(xlToLeft).Column ' Gives you total columns in a sheet
Next
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have around 500 files of excel and the most part have filter. Now i need to put all information in one sheet. I found how to put all in one sheet but the books with filter only copy information with filter and i need all.
So, i need to remove filter of all excel files. Important (not all files have filter)
Thanks for your help ;)
Before you copy the data, you should use:
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
This will turn off the filter.
Will remove any filter on all opened workbooks, have fun :)
sub try_this()
dim wb as workbook
dim sh as worksheet
for each wb in workbooks
for each sh in wb.worksheets
sh.AutoFilterMode = False
next
next
end sub
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have list of Excel file paths in a text file (output from a VBA macro).
How can I use vba to open these files?
Thanks
Something like this which will skip over invalid filenames/paths
It opens any valid workbooks stored in a textfile, C:\temp\test.txt, then closes them in turn
Sub Alistair_Cooked()
Dim objFSO As Object
Dim objWB As Workbook
Dim strFN As String
Dim objTF As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTF = objFSO.OpenTextFile("C:\temp\test.txt")
On Error Resume Next
Do While Not objTF.AtEndOfStream
strFN = objTF.readline()
Set wb = Workbooks.Open(strFN)
If wb Is Nothing Then
Debug.Print strFN
Else
'do something
wb.Close False
Set wb = Nothing
End If
Loop
On Error GoTo 0
End Sub
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Does anyone know if it is possible to loop through an Excel spreadsheet using VBA without using ActiveCell?
As an example, how can you create the COUNTIF function from scratch in VBA without using ActiveCell (or calling on the COUNTIF function, obviously)?
I want to avoid ActiveCell because it seems like an unnecessary use of resources to scroll the active cell around when typically you're trying to manipulate a simple matrix, especially when looping through thousands of cells.
Dim c as Range
For Each c in Sheets("Sheet1").Range("A1:A1000").Cells
'do something with c
Next c
What Tim said.
Just to address the count-if portion of your question, here is a way to do it without using the formula:
Sub Macro14()
Dim c As Range
Dim rng As Range
Dim count_if As Integer
Set rng = Sheets("Sheet1").Range("A1:A1000")
For Each c In rng
If c = "Apple" Then
count_if = count_if + 1
End If
Next c
Debug.Print count_if
End Sub
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I currently have a vlookup populating row 1 (cells G1-AZ1) with titles, and would like to hide the Columns(G1-AZ1) if the vlookup pulls back nothing/#N/A. I know this is a simple macro but I'am new to VBA and I have had zero luck searching the web.
Thanks!
I usually place such formulas in ISNA() and then just use Excel filter to hide empty rows
=IF(ISNA(VLOOKUP(A3,G1:H7,2,FALSE)),"",VLOOKUP(A3,G1:H7,2,FALSE))
Try this:
Loop throught he header cells
Set the EntireColumn.Hidden property based on your criteria
Use .ScreenUpdating = False to prevent screen flicker and speed it up
Sub HideColumns()
Dim rng As Range
Dim cl As Range
Application.ScreenUpdating = False
Set rng = [G1:AZ1]
For Each cl In rng
If IsError(cl) Then
cl.EntireColumn.Hidden = cl = CVErr(xlErrNA)
Else
cl.EntireColumn.Hidden = cl = ""
End If
Next
Application.ScreenUpdating = True
End Sub