Results 1 to 6 of 6

How to properly qualify a range

Threaded View

ungers How to properly qualify a... 10-08-2013, 08:34 PM
TMS Re: How to properly qualify a... 10-08-2013, 10:48 PM
ungers Re: How to properly qualify a... 10-09-2013, 10:06 AM
TMS Re: How to properly qualify a... 10-09-2013, 10:11 AM
ungers Re: How to properly qualify a... 10-09-2013, 11:04 AM
TMS Re: How to properly qualify a... 10-09-2013, 11:14 AM
  1. #1
    Registered User
    Join Date
    10-04-2013
    Location
    Liverpool
    MS-Off Ver
    Excel 2003
    Posts
    27

    How to properly qualify a range

    Hi,

    I have written some code to export queries from access into excel and then apply some formatting.

    Private Sub Export_Quarterly_Click()
    
    
    Dim strPathFile As String
    Dim strTable As String, strBrowseMsg As String
    Dim strFilter As String, strInitialDirectory As String
    Dim blnHasFieldNames As Boolean
    
    strBrowseMsg = "Save Complete Records Report To:"
    strInitialDirectory = "C:\Users\......\Quarterly VAT Records"
    strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xls)", "*.xls")
    
    strPathFile = ahtCommonFileOpenSave(InitialDir:=strInitialDirectory, _
          Filter:=strFilter, OpenFile:=False, _
          DialogTitle:=strBrowseMsg, _
          Flags:=ahtOFN_OVERWRITEPROMPT)
    
    If strPathFile = "" Then
          MsgBox "Report was not saved", vbOK, "No Selection"
          Exit Sub
    End If
    
    DoCmd.TransferSpreadsheet 1, 8, "Invoice Record 2", strPathFile, True
    DoCmd.TransferSpreadsheet 1, 8, "VAT Record", strPathFile, True
    DoCmd.TransferSpreadsheet 1, 8, "Recharge VAT Record", strPathFile, True
    
    Set XL = CreateObject("Excel.Application")
    XL.Visible = True
    XL.UserControl = True
    Set WB = XL.Workbooks.Open(strPathFile)
    Set ws1 = WB.Worksheets(1)
    Set ws2 = WB.Worksheets(2)
    Set ws3 = WB.Worksheets(3)
    
    Dim intCountofSheets, intCurrentSheet As Integer
    
    With ws1
    .Name = "Income"
    .Range("A1") = "Invoice Date"
    .Range("B1") = "Invoice Number"
    .Range("C1") = "Net Amount"
    lastrow = Range("A1").End(xlDown).Row
    .Range(Range("B2"), Range("B" & lastrow)).NumberFormat = "\D\U\A\L0##\/\/###"
    .Range("A" & lastrow + 2).value = "Net Total :"
    .Range("C" & lastrow + 2).Formula = "=SUM(C2:C" & lastrow & ")"
    End With
    
    With ws2
    .Name = "VAT"
    .Range("A1") = "Payment Date"
    .Range("B1") = "Invoice Number"
    .Range("C1") = "Invoice Amount"
    .Range("D1") = "VAT Rate"
    .Range("E1") = "VAT Received"
    End With
    
    XL.DisplayAlerts = False
    With ws3
    If IsEmpty(.Range("A2").value) Then
    .Delete
    Else
    .Name = "Recharged VAT"
    .Range("A1") = "Payment Date"
    .Range("B1") = "Invoice Number"
    .Range("C1") = "Recharge"
    .Range("D1") = "Details"
    .Range("E1") = "Amount"
    .Range("F1") = "VAT Rate"
    .Range("G1") = "VAT Received"
    End If
    End With
    XL.Application.DisplayAlerts = True
    
    intCountofSheets = WB.Sheets.Count
    intCurrentSheet = 1
    
    Do While intCurrentSheet <= intCountofSheets
    
    WB.Worksheets(intCurrentSheet).Activate
    lastrow = Range("A1").End(xlDown).Row
    
    If intCurrentSheet = 2 Then
    Range(Range("B2"), Range("B" & lastrow)).NumberFormat = "\D\U\A\L0##\/\/###"
    Range(Range("D2"), Range("D" & lastrow)).NumberFormat = "0.0%"
    Range("E2").Formula = "=(C2*D2)"
    Range("E2").Copy
    Range("E2:E" & lastrow).PasteSpecial
    Range(Range("E2"), Range("E" & lastrow + 2)).Select
    Range("A" & lastrow + 2).value = "Net Total :"
    Range("E" & lastrow + 2).Formula = "=SUM(E2:E" & lastrow & ")"
    Range(Range("E2"), Range("E" & lastrow + 2)).Select
    With Selection
    .Style = "Currency"
    .NumberFormat = "$#,##0.00"
    End With
    End If
    
    If intCurrentSheet = 3 Then
    Range(Range("B2"), Range("B" & lastrow)).NumberFormat = "\D\U\A\L0##\/\/###"
    Range(Range("F2"), Range("F" & lastrow)).NumberFormat = "0.0%"
    Range("G2").Formula = "=(E2*F2)"
    Range("G2").Copy
    Range("G2:G" & lastrow).PasteSpecial
    Range("A" & lastrow + 2).value = "Recharged VAT Total :"
    Range("G" & lastrow + 2).Formula = "=SUM(G2:G" & lastrow & ")"
    Range(Range("G2"), Range("G" & lastrow + 2)).Select
    With Selection
    .Style = "Currency"
    .NumberFormat = "$#,##0.00"
    End With
    End If
    
    Columns("A").Select
    Selection.EntireColumn.Insert
    Columns("A").Select
    Selection.ColumnWidth = 2
    Cells.Select
    With Selection.Font
    .Name = "Tahoma"
    .Size = 10
    End With
    Rows("1:1").Select
    With Selection
    .HorizontalAlignment = xlCenter
    .EntireRow.Insert
    .EntireRow.Insert
    End With
    Rows("1:1").Select
    With Selection
    .RowHeight = 26
    With .Font
    .Size = 20
    .Bold = True
    End With
    End With
    Rows("3:3").Select
    With Selection.Font
    .Bold = True
    .Size = 12
    End With
    
    Range("B3").End(xlDown).Offset(2, 0).EntireRow.Select
    With Selection.Font
    .Bold = True
    .Size = 12
    End With
    
    Range(Rows("4:4"), Rows("4:4").End(xlDown)).Select
    Selection.HorizontalAlignment = xlRight
    
    Columns.EntireColumn.Autofit
    
    Range("A1").Select
    Selection.value = ActiveSheet.Name
    
    Range("B2").Select
    
    intCurrentSheet = intCurrentSheet + 1
    Loop
    
    ws1.Activate
    WB.Save
    
    Set XL = Nothing
    Set WB = Nothing
    Set ws1 = Nothing
    Set ws2 = Nothing
    Set ws3 = Nothing
    
    End Sub
    Now this works perfectly fine but I know it isn't quite good practice.

    In the first with ws1 section I understand that the lastrow = ...part works because it is in the currently active sheet. And then later down I have had to put some formatting into the If intcurrent = 2 and If intcurrent = 3 sections because I get an error if I bring them up under the with ws2 and with ws3 sections and use the lastrow reference again because, one it is referencing the first sheet, and 2 because there is no object properly set.

    So my question is how can I neaten this up and what is the code for properly setting references to the ranges. Ideally i'd define the range Range("A1").End(xlDown).Row for each worksheet.

    Thanks in advance!
    Last edited by ungers; 10-27-2013 at 12:51 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. text to pre-qualify from a list
    By rdyteves in forum Excel General
    Replies: 2
    Last Post: 12-12-2011, 08:11 PM
  2. Checking 3+ ranges to qualify a value
    By Masrim in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-28-2011, 04:36 PM
  3. function to qualify plain text rows before importing (xlFixedWidth)
    By reteptnarg in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-15-2010, 08:01 PM
  4. [SOLVED] How to create a table to qualify a qty discount in exel
    By 1image in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 10-12-2005, 11:05 AM
  5. Query on Date range does not qualify the Year
    By Malcolm Makin in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-23-2005, 02:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1