VBA runs but doesn't do anything

Temporary-Failure

Board Regular
Joined
Jul 16, 2010
Messages
140
I have workbook Laskutusohjelma.xls where is button which runs macro called "Check" and opens also Varastohallinta.xls file. In the Varastohallinta.xls there is a code in the ThisWorkbook:

Code:
Public Sub Workbook_BeforeClose(Cancel As Boolean)
    Call teecsv
    Worksheets("Varastohallinta").Activate
    ActiveWorkbook.Save
    Me.Saved = True
End Sub

Teecsv is in module in the Varastohallinta.xls:
Code:
Public Sub teecsv()

    Workbooks("Varastokirjanpito.xls").Activate
    
    
    Workbooks("Varastokirjanpito.xls").Sheets("Omat").Select
    Columns("A:K").Select
    Selection.ClearContents
    Range("A1").Select
    Sheets("Varastohallinta").Select
    
    Range("P2:P1838").Select
    Selection.Copy
    Workbooks("Varastokirjanpito.xls").Sheets("Omat").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
    Dim LastRow As Long
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    'MsgBox LastRow
    Range("A" & LastRow).Select
'----------------------------------------------------------------------------------------------
Workbooks("Varastokirjanpito.xls").Sheets("Varastohallinta").Select
    
    Range("Q2:Q1838").Select
    Selection.Copy
    Workbooks("Varastokirjanpito.xls").Sheets("Omat").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    'MsgBox LastRow
    Range("A" & LastRow).Select
'----------------------------------------------------------------------------------------------
Workbooks("Varastokirjanpito.xls").Sheets("Varastohallinta").Select
    
    Range("R2:R1838").Select
    Selection.Copy
    Workbooks("Varastokirjanpito.xls").Sheets("Omat").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    'MsgBox LastRow
    Range("A" & LastRow).Select
'----------------------------------------------------------------------------------------------
Workbooks("Varastokirjanpito.xls").Sheets("Varastohallinta").Select
    
    Range("S2:S1838").Select
    Selection.Copy
    Workbooks("Varastokirjanpito.xls").Sheets("Omat").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    'MsgBox LastRow
'----------------------------------------------------------------------------------------------
Workbooks("Varastokirjanpito.xls").Sheets("Varastohallinta").Select
    
    Range("T2:T1838").Select
    Selection.Copy
    Workbooks("Varastokirjanpito.xls").Sheets("Omat").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    'MsgBox LastRow

    Range("A1" & ":" & "A" & LastRow).Select
    Selection.Value = Selection.Value
    
        Application.CutCopyMode = False
    Selection.Sort Key1:=Range("A1"), Order1:=xlDescending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortTextAsNumbers
    Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortTextAsNumbers
    Range("A1").Select

    Application.DisplayAlerts = False

    ChDir _
        "E:\Copiot O - V\Omat tiedostot\Yritys\Verkkokauppa Clover Shop Pro\Nettihotellipalvelimelta imutettu verkkokauppa\CSV"
    ActiveWorkbook.SaveAs Filename:= _
        "E:\Copiot O - V\Omat tiedostot\Yritys\Verkkokauppa Clover Shop Pro\Nettihotellipalvelimelta imutettu verkkokauppa\CSV\Omat.prn" _
        , FileFormat:=xlTextPrinter, CreateBackup:=False
        
    ChDir "E:\Copiot O - V\Omat tiedostot\Yritys\Laskut ja kuitit"
    ActiveWorkbook.SaveAs Filename:= _
        "E:\Copiot O - V\Omat tiedostot\Yritys\Laskut ja kuitit\Varastokirjanpito.xls" _
        , FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False


GivenLocation = "E:\Copiot O - V\Omat tiedostot\Yritys\Verkkokauppa Clover Shop Pro\Nettihotellipalvelimelta imutettu verkkokauppa\CSV\" 'note the trailing backslash
OldFileName = "Omat.prn"
NewFileName = "Omat.csv"

If Dir(GivenLocation & NewFileName) <> "" Then
    GoTo remove
Else
    GoTo teeuusi
End If

remove:
Kill GivenLocation & NewFileName

teeuusi:
Name GivenLocation & OldFileName As GivenLocation & NewFileName


Call uploadcsv

Application.DisplayAlerts = True
Workbooks("Varastokirjanpito.xls").Sheets("Varastohallinta").Select
Range("C1").Select

End Sub

If I run teecsv macro directly in the Varastohallinta.xls it works perfectly but If I run Check-macro in the Laskutusohjelma, then teecsv runs through but it doesn't do anything, not a single line what is in the teecsv-macro. Why?
 
Last edited:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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