Re: Overflow 6 in code - why, and how to solve?
Hi Maria,
Welcome to the forum.
Please remember to use code tags when posting code - I've added them for you this time.
Re: Overflow 6 in code - why, and how to solve?
Hi Maria,
Welcome to the forum.
Please remember to use code tags when posting code - I've added them for you this time.
Re: Assign a macro
As regards the question, I think you'll find it easier if you work with a workbook variable so that you can easily reference the appropriate workbook.
Re: Assign a macro
Please remember to use Code tags, not simply use Bold font. Thanks.
Re: Find value in range copy nth cell value to another sheet
Hi and welcome to the forum.
Take a look at Offset. This will enable you to refer to a range by offsetting the desired number of rows and columns from an existing range. For example, the following refers to C2 by offset from A1:
Re: On Error GoTo Issue, please help!
Hi,
You've already had some feedback regarding the general structure of your code so I'll just comment on the error-handling part.
When an error arises it effectively flips a switch so that error mode is 'on'. Whilst in error mode any subsequent errors will not be handled (which is what you were experiencing). The way to switch it 'off' again lies in the use of Resume (or exiting the sub). It's also good practice to have your error-handling routines at the end of a sub.
Compare the following:
Sub To_Err_Is_Human()
Dim rngCell As Range
On Error GoTo ErrHandler
TheStart:
MsgBox Err.Number
Set rngCell = Cells(0, 0)
Exit Sub
ErrHandler:
MsgBox Err.Number
GoTo TheStart
End Sub
Sub To_Err_Is_Human2()
Dim rngCell As Range
On Error GoTo ErrHandler
TheStart:
MsgBox Err.Number
Set rngCell = Cells(0, 0)
Exit Sub
ErrHandler:
MsgBox Err.Number
Resume TheStart
End Sub
Display More
Re: User defined variables in a path reference
Hi Matt,
You could take a look at using the 'Indirect' worksheet function. It does, however, have a fairly significant drawback - it won't work as a link to closed workbooks.
There have been various 'fixes' attempted by VBA coders over the years. For example, the 'Indirect.ext' function found within the Morefunc add-in and also a UDF called 'Pull' (I'm afraid I can't remember who wrote that. Anyone else remember?). However, I believe that these fixes have 'issues' with the most current version of Excel - ie they no longer work!
You could, perhaps, try combing Indirect with the use of a VBA routine that would open all of the associated workbooks to update the data and then close them again. It's not very elegant but it may work if you don't have too many workbooks to work with.
EDIT: Found a link that to a YouTube video that discusses the Pull function (it was written by Harlan Grove)
http://www.youtube.com/watch?v=crbGfqEorXQ
Re: Macros, Copy, Paste, Condition
Hi and welcome to the forum.
How about something like this:
Sub Find_Call()
Dim wsMain As Worksheet, wsBackup As Worksheet
Dim rngCell As Range, strFirst As String
Set wsMain = ThisWorkbook.Worksheets("main")
Set wsBackup = ThisWorkbook.Worksheets("backup")
'the worksheets
With wsMain.Cells
Set rngCell = .Find(what:="call", LookIn:=xlValues)
'look for target text
If Not rngCell Is Nothing Then
'if found, proceed with copying
strFirst = rngCell.Address
Do
rngCell.EntireRow.Copy _
wsBackup.Range("A" & wsBackup.Cells(wsBackup.Rows.Count, "A").End(xlUp).Row + 1)
Set rngCell = .FindNext(rngCell)
Loop While Not rngCell Is Nothing And rngCell.Address <> strFirst
End If
End With
End Sub
Display More
If you are new to VBA then a good tip is to try using the macro recorder to record the process that you want to code. It won't be exactly what you want but it is often a good starting point.
Re: VBA: Worksheet_Change(ByVal Target As Range) in 2 cells + copy and paste to 2 Col
muggi,
If you are cross-posting a query at another forum then please post a link to your post at that forum. (See the link in my signature for the reason why we ask that posters do this). Thank you.
Re: Problem with For each
I agree with pike, UsedRange can be a little unpredictable at times. How about just defining the range in column A that you want to use. Something like this perhaps:
Sub c4000lc()
Dim c As Range, rngData As Range
Dim wsData As Worksheet, wsPrint As Worksheet
Set wsData = ThisWorkbook.Worksheets("4numbers")
Set wsPrint = ThisWorkbook.Worksheets("C4000LC")
'the worksheets we are using
With wsData
Set rngData = .Range("A1:A" & .Cells(Rows.Count, "A").End(xlUp).Row)
'MsgBox rngData.Address
End With
'an alternative to UsedRange
For Each c In rngData
c.Copy Destination:=wsPrint.Range("h30")
wsPrint.PrintOut Copies:=1, ActivePrinter:="GlobalMark Color & Cut (Copy 2) on COM1:"
Next
'loop through, as before
End Sub
Display More
Re: Viewing application.ontime scheduled events
Hi,
I'm guessing, based upon the first and last blocks of code within the BeforeClose event code that you posted, that this is a trimmed-down version of your code. Could it be that any of the other code is causing trouble (especially as you are driving the OnTime routine from a number of events and the BeforeClose event switches off EnableEvents)? Just a thought.
The key with OnTime is passing the exact time in Ending it as was used to Start it. Could the ScheduledTime variable be being changed somewhere such that the time when switching off OnTime doesn't match the time when it was switched on?
Re: look up value in another sheet by referencing a cell containing a sheet name
Hi and welcome to the forum.
If Sheet1 Cells D3 contains the text Sheet10, then to return the value from Cell A1 on Sheet10 you would enter:
=INDIRECT("'" & D3 & "'!" &"A1")
Note that the D3 cell reference is surrounded by a single quote (within double quotes) on the left and a single quote and exclamation mark (again within double quotes) on the right.
Re: VBA Automate Unzip with SecureZip
Hi and welcome to the forum.
I know when I wrote a Winzip routine a while ago it was fairly straight-forward to modify the code for Winrar (see below). What code have you got and what have you tried so far?
'******************************************************************
'Using WinRar within Excel
'Syntax
'
' RAR <command> [ -<switches> ] <archive> [ <@listfiles...> ]
' [ <files...> ] [ <path_to_extract\> ]
'******************************************************************
'*************
' Adding files
'*************
Sub WinRarIt()
Dim WinRarPath As String 'WinRar.exe location
Dim RarIt As String 'Command line instruction
Dim SourceDir As String 'The source directory
Dim SourceFile As String 'The source file
Dim Source As String 'The combined Rar from path(s)(FROM)
Dim DestDir As String 'The Rarped file directory
Dim DestRarName As String 'The Rarped file
Dim Dest As String 'The combined Rar to path (TO)
'*** Check installation of WinRar ***
WinRarPath = "C:\Program Files\WinRar\"
If Dir(WinRarPath, vbDirectory) = "" Then
MsgBox "WinRar is not installed in the default directory." _
& Chr$(13) & "Archiving of files will not be possible."
Exit Sub
End If
'*** Set the source details ***
SourceDir = ThisWorkbook.Path & "\"
SourceFile = "This has spaces.xls"
Source = SourceDir & "\" & SourceFile
'If source name has one or more spaces surround it with ""
If InStr(1, Source, " ", vbTextCompare) <> 0 Then Source = Chr(34) & Source & Chr(34)
'*** Set the destination details
DestDir = "C:\Rarped Excel Files"
'check that it exists
If Dir(DestDir, vbDirectory) = "" Then MkDir DestDir
DestRarName = "Test.Rar"
Dest = DestDir & "\" & DestRarName
If InStr(1, Dest, " ", vbTextCompare) <> 0 Then Dest = Chr(34) & Dest & Chr(34)
'*** Do the Rarping ***
RarIt = Shell(WinRarPath & "WinRar.exe a " & Dest & " " & Source, vbNormalFocus)
End Sub
'*****************
' Extracting files
'*****************
Sub UnWinRarIt()
Dim WinRarPath As String 'WinRar.exe location
Dim RarIt As String 'Command line instruction
Dim SourceDir As String 'The source directory
Dim SourceRarFile As String 'The source file
Dim Source As String 'The combined Rar from path(s)(FROM)
Dim Dest As String 'The combined unRar to path (TO)
WinRarPath = "C:\Program Files\WinRar\"
If Dir(WinRarPath, vbDirectory) = "" Then
MsgBox "WinRar is not installed in the default directory." _
& Chr$(13) & "Archiving of files will not be possible."
Exit Sub
End If
SourceDir = "C:\Rarped Excel Files"
SourceRarFile = "Test.Rar"
Source = SourceDir & "\" & SourceRarFile
If InStr(1, Source, " ", vbTextCompare) <> 0 Then Source = Chr(34) & Source & Chr(34)
Dest = "C:\UnRarped Excel Files\"
If Dir(Dest, vbDirectory) = "" Then MkDir Dest
If InStr(1, Dest, " ", vbTextCompare) <> 0 Then Dest = Chr(34) & Dest & Chr(34)
RarIt = Shell _
(WinRarPath & "WinRar.exe e " & Source & " " & Dest, vbNormalFocus)
End Sub
Display More
Re: VBA code for updating master workbook with data in raw file
Hi VarunV
Actually you are not using a worksheet variable - Sname is a Variant variable that you are using to hold details of the unique ID data. Try using Workbook variables for the workbooks that you are working with and Worksheet variables for the sheets within them. If I get time I'll re-work your routine to show what I mean but you should give it a go for yourself.
In the meantime, the following should help to illustrate what I mean. (Note that I've used Activeworkbook in the illustration just for speed after downloading your example workbook - I would normally open it by assigning it to a workbook variable. For example, declare a variable like this : Dim wbkRaw As Workbook and then open the Raw workbook like this : Set wbkRaw = Workbooks.Open(Filename:=NewFN) - much easier to reference this way.)
Sub test_ws_add()
Dim rngRaw As Range, rngCell As Range
Dim wsRaw As Worksheet
Dim wsNew As Worksheet
Set wsRaw = ActiveWorkbook.Worksheets("Counts")
Set rngRaw = wsRaw.Range("A2:A" & wsRaw.Cells(wsRaw.Rows.Count, "A").End(xlUp).Row)
'set range of unique IDs
For Each rngCell In rngRaw
If Not SheetExists(rngCell.Value) Then
Set wsNew = Worksheets.Add
wsNew.Name = rngCell.Value
End If
Next rngCell
'loop through each unique ID and add sheet if doesn't already exist
End Sub
Function SheetExists(strWSname As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = Worksheets(strWSname)
On Error GoTo 0
If Not ws Is Nothing Then SheetExists = True
End Function
Display More
Re: Creating new new worksheet from from a list
Hi and welcome to the forum.
I haven't had time to test this nor to add all of the things that you want but it should help get you started.
Sub Main()
Dim rngToTest As Range, rngUnique As Range, rngCell As Range
Dim wsNew As Worksheet
Application.ScreenUpdating = False
With Worksheets("Sheet1")
Rows("1:1").Insert
.Range("A1").Value = "Dummy header"
'add header rows for Advanced Filter
Set rngToTest = .Range("A1:A" & .Cells(Rows.Count, "A").End(xlUp).Row)
rngToTest.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("G1"), Unique:=True
'copy the unique entries to another range (amend "G1" to suit)
Rows("1:1").Delete
'remove header rows
Set rngUnique = .Range("G1:G" & .Cells(Rows.Count, "G").End(xlUp).Row)
'establish a reference to the unique range and the range at its right
For Each rngCell In rngUnique
If Not SheetExists(rngCell.Value) Then
Set wsNew = Worksheets.Add
wsNew.Name = rngCell.Value
'do stuff
End If
'add new sheet if not already present
Next rngCell
'loop through the unique range
End With
Application.ScreenUpdating = True
End Sub
Function SheetExists(strWSname As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = Worksheets(strWSname)
On Error GoTo 0
If Not ws Is Nothing Then SheetExists = True
End Function
Display More
Re: VBA code for updating master workbook with data in raw file
Hi again and thanks for amending the post.
I'd suggest a couple of things that may help make things a little easier.
1. Make use of a worksheet variable. It's much easier to make reference to the intended object that way.
2. Try using simple functions to undertake tests like seeing whether a worksheet already exists.
Combing the above might give us something like the code below. It makes use of a worksheet variable, wsNew, and a function that tests for the existence of a worksheet - in this case, if the sheet name doesn't already exist then a new sheet is added and given that name.
Sub test_ws_add()
Dim strWSname As String
Dim wsNew As Worksheet
strWSname = "Sheet4"
If Not SheetExists(strWSname) Then
Set wsNew = Worksheets.Add
wsNew.Name = strWSname
End If
End Sub
Function SheetExists(strWSname As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = Worksheets(strWSname)
On Error GoTo 0
If Not ws Is Nothing Then SheetExists = True
End Function
Display More
Re: VBA code for updating master workbook with data in raw file
Hi,
I don't know what sort of formatting you were aiming for but ... it's not working. Please just re-post your code remembering to add code tags around it (and nothing else). Thanks.
Re: looking up files and deleting
Hi John,
You could try something like the code below. The code has not been tested - given that it involves the deletion of files I strongly suggest that you test it first on some dummy data in a dummy directory.
Sub ByeBye()
Const strDir As String = "C:\Documents and Settings\Richie\My Documents\Excel\Tests\"
'the path to the directory containing the files
Dim wsFiles As Worksheet
Dim rngFiles As Range, rngCell As Range
Set wsFiles = ThisWorkbook.Worksheets("Sheet1")
Set rngFiles = wsFiles.Range("A1:A250")
'the worksheet and range contaning the list of files
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
'speed-up process
For Each rngCell In rngFiles
Kill strpath & rngCell.Value
Next rngCell
'loop through the file names
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With
'reset
End Sub
Display More
Re: Status Bar
Quote from Shums;620117Richie thanks for your response, but I am not VBA expert, I am just a beginner. ...
In that case I would offer two bits of advice (just my opinion - you may see it differently):
1. Don't blindly copy bits of code from multiple sources and then put it all together and expect it to work. Try to understand how the code works and how it can be used to achieve your objective.
2. If you want to progress from being a beginner you need to try to improve. It doesn't matter if you don't get it right first time - we often learn by our mistakes - but if somebody points you in the right direction you should at least give it a go.
Re: Look at cell value and split file
Hey, you've made some good progress
How about the following:
Sub Subord(strFullName As String)
Const strDate As String = "12/30/2004"
Dim wbk As Workbook, rngFound As Range, wbkNew As Workbook
Set wbk = Workbooks.Open(strFullName)
'open the workbook to work with
With wbk.ActiveSheet.Cells
Set rngFound = .Find(what:=DateValue(strDate), LookIn:=xlFormulas)
'search for date
If Not rngFound Is Nothing Then
Set wbkNew = Workbooks.Add
.Range("A1:V" & rngFound.Row).Copy
With wbkNew
.ActiveSheet.Paste
.SaveAs wbk.Path & Application.PathSeparator & Left(wbk.Name, Len(wbk.Name) - 4) & "1" & ".xls"
.Close
End With
.Range("A2:V" & rngFound.Row).EntireRow.Delete
.SaveAs wbk.Path & Application.PathSeparator & Left(wbk.Name, Len(wbk.Name) - 4) & "2" & ".xls"
End If
'if date found proceed
End With
End Sub
Display More