+ Reply to Thread
Results 1 to 3 of 3

Can't declare FinalRow

Hybrid View

  1. #1
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Can't declare FinalRow

    I can't seem to get FinalRow to declare? I am trying to put in a totals row, but it just skips over that bit of code. I am stumped, as I have never had this problem before.

    Option Explicit
    
    Sub CopyData()
    Dim Val As Range, rnge As Range, wbDataPath As String
    Dim wsDest As Worksheet, wsData As Worksheet, wbData As Workbook
    Dim FinalRow As Long, FinalCol As Long
    
    
    
    
    'Prompts window to open file
        wbDataPath = Application.GetOpenFilename("Excel & CSV Files, *.xls*; *.csv")
        If wbDataPath = "False" Then Exit Sub
        Application.ScreenUpdating = False
        
    'set each object
        Set wsDest = ActiveWorkbook.ActiveSheet
        Set wbData = Workbooks.Open(wbDataPath)
        Set wsData = wbData.Sheets(1)
        
        FinalRow = wsDest.Cells(Rows.Count, 1).End(xlUp).Row
        FinalCol = wsDest.Cells(2, Columns.Count).End(xlToLeft).Column
        
        With wsData
    'Unhide Columns
            .Columns.EntireColumn.Hidden = False
    
        'Find not needed columns and delete
            For Each Val In .UsedRange
                If Val.Value = "選択" Or Val.Value = "利用者コード" Or Val.Value = "フリガナ" Or Val.Value = "部屋コード" Or Val.Value = "部屋グループコード" Or Val.Value = "部屋グループ名称" Or Val.Value = "介護保険者番号" Or Val.Value = "被保険者番号" Or Val.Value = "保険者名称" Or Val.Value = "広域番号" Or Val.Value = "広域名称" Or Val.Value = "介護度コード" Or Val.Value = "介護度名称" Or Val.Value = "地区コード" Or Val.Value = "地区名称" Or Val.Value = "医療保険者番号" Or Val.Value = "記号" Or Val.Value = "番号" Or Val.Value = "契約日数" Or Val.Value = "サービス実数" Or Val.Value = "外泊日数" Then
                    If rnge Is Nothing Then Set rnge = Val Else Set rnge = Union(rnge, Val)
                End If
            Next Val
        
            If Not rnge Is Nothing Then rnge.EntireColumn.Delete
        ' Copy contents to destination sheet
            .Range("A:A").SpecialCells(xlConstants).EntireRow.Copy
                    wsDest.Range("A1").PasteSpecial xlPasteAll, Transpose:=True
            
        End With
        wbData.Close False      'close opened file with no changes
        
        'Add Total Row to wsDest sheet
         'wsDest.Cells(45, 1).Value = "Total"
         'wsDest.Range(Cells(45, 2), Cells(45, FinalCol)) _
            .FormulaR1C1 = "=Sum(R[-42]C:R[-2]C)"
            
        wsT.Cells(FinalRow + 2, 1).Value = "Total"
        wsT.Range(Cells(FinalRow + 1, 2), Cells(FinalRow + 1, FinalCol)) _
            .FormulaR1C1 = "=Sum(R[" & -FinalRow + 2 & "]C:R[-2]C)"
                            
            
        With wsDest
                .Columns("B").EntireColumn.Delete
                .Columns("A:CC").EntireColumn.AutoFit
                .Rows.EntireRow.RowHeight = 13
        End With
        
        wsDest.Range("B3", "CC10000").Style = "Comma [0]"
         Range("A2:BX2615").Select
        With Selection.Font
            .Name = "MS 明朝"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .TintAndShade = 0
        End With
        
         
    
        Application.ScreenUpdating = True
    End Sub
    The first workbook holds the data, The second brings in the data.You will have to take of the comment tags to get the final row peice of code to run. I have also hard coded the formula to see if it would work, that is why there is two pieces of code doing exactly the same thing.
    Attached Files Attached Files
    Last edited by JapanDave; 11-13-2011 at 02:02 AM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,936

    Re: Can't declare FinalRow

    I don't understand your problem. The code debugs without errors and sets finalrow and finalcol each to one since sheet1 is empty.
    Ben Van Johnson

  3. #3
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Can't declare FinalRow

    Quote Originally Posted by protonLeah View Post
    I don't understand your problem. The code debugs without errors and sets finalrow and finalcol each to one since sheet1 is empty.
    Ahh, I have been working on this code for too long. Thanks you have pointed out where I was going wrong, I have the finalRow/Finalcol is the wrong place in the code. Now it is working!

    Cheers.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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