Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-05-2018, 06:47 AM
slaycock slaycock is offline Find last non empty cell (moving horizontally), then move one cell to the right Windows 7 64bit Find last non empty cell (moving horizontally), then move one cell to the right Office 2016
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

Will you be selecting the row in the table before you run the macro or will you just expect the macro to process Table(8).

Providing more information goes both ways you know.



Code:
Public Function find_last_empty_cell_in_range(this_range As Word.Range) As Long
' Searches through the cells in the table and returns the index of the first empty cell.
' The input parameter is a word range so the function is generic for a table
' or a selection within a table
'
' This function will work with non uniform tables
' Scanning a table using the range.cell(x,y) method will fail with non-uniforma tables
'
' We test if the length of the text in the cell is 2 or less characters to find an empty cell.
' This is because Cells contain hidden characters which mark the end of the cell
' and end of the text (I think)

Dim my_cell                                         As Word.Cell
Dim my_index                                        As Long

    For my_index = 1 To this_range.Cells.Count
        With this_range
            If Len(.Cells(my_index).Range.Text) <= 2 Then
                find_last_empty_cell_in_range = my_index
                Exit Function
            End If
        End With
    Next
End Function

Sub find_cell()

Dim my_range                            As Word.Range

    Set my_range = ActiveDocument.Tables(8).Range

    Debug.Print find_last_empty_cell_in_range(my_range)
 
End Sub
Reply With Quote
  #2  
Old 07-05-2018, 07:13 AM
klutch klutch is offline Find last non empty cell (moving horizontally), then move one cell to the right Windows 7 64bit Find last non empty cell (moving horizontally), then move one cell to the right Office 2016
Advanced Beginner
Find last non empty cell (moving horizontally), then move one cell to the right
 
Join Date: Jun 2018
Posts: 31
klutch is on a distinguished road
Default

Code:
Sub CopyAndPaste()
    Dim myfile, wdApp As New Word.Application, wDoc As Word.Document
    'select truck report file
   ChDrive "E:\"
ChDir "E:\WG\TVAL\"
myfile = Application.GetOpenFilename(, , "Browse for Document")
    Dim i As Integer
   'searches for row with "avg" then selects column E(avg of temperature mean) of that row.
    i = Application.Match("Avg", Sheet1.Range("A1:A20"), 0)
   
    'makes the file appear
    wdApp.Visible = True
    Set wDoc = wdApp.Documents.Open(myfile)
    
    With wDoc


Dim my_cell                                         As Word.Cell
Dim my_index                                        As Long

    For my_index = 1 To this_range.Cells.Count
        With this_range
            If Len(.Cells(my_index).Range.Text) <= 2 Then
                find_last_empty_cell_in_range = my_index
                Exit Sub
            End If
        End With
    Next
End With
Dim my_range                            As Word.Range

    Set my_range = ActiveDocument.Tables(8).Range

    Debug.Print find_last_empty_cell_in_range(my_range)
 
End Sub
This is how I added that into my code and got an error on the line
Code:
 For my_index = 1 To this_range.Cells.Count
I have found a way to select the column that I want to go to with
Code:
wDoc.Tables(8).Columns(3).Select
but how do I select the first empty cell?
I found this online
Code:
Dim x As Long
Dim crange As Range
With Selection
    For x = 1 To .Cells.Count
        Set crange = .Cells(x).Range
        With crange
            
            If Len(.Text) <> 0 Then
                .Select
                Exit For
            End If
        End With
    Next x
End With
Selection.Cells(1).Select

Selection.TypeText Text:=Range("e" & i)
but it gives me error as well and only selects the entire column opposed to the first empty cell
Reply With Quote
  #3  
Old 07-05-2018, 09:09 AM
klutch klutch is offline Find last non empty cell (moving horizontally), then move one cell to the right Windows 7 64bit Find last non empty cell (moving horizontally), then move one cell to the right Office 2016
Advanced Beginner
Find last non empty cell (moving horizontally), then move one cell to the right
 
Join Date: Jun 2018
Posts: 31
klutch is on a distinguished road
Default

Code:
 With wDoc
Dim ocolumn As Column
Dim ocell As Cell

For Each ocell In wDoc.Tables(8).Columns(3)
If Len(ocell.Range) = 2 Then
 ocell.Select
 Selection.PasteSpecial
 End If
 Next ocell
 End With
I am looking for something like this, but this does not have the right object property
Reply With Quote
  #4  
Old 07-05-2018, 09:56 AM
slaycock slaycock is offline Find last non empty cell (moving horizontally), then move one cell to the right Windows 7 64bit Find last non empty cell (moving horizontally), then move one cell to the right Office 2016
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

That's not the way to use the code I posted.

I provided a function to find the first empty cell in a table which you seem not to know how to use.

I provided an example of how to call the function, which you haven't understood.

You are writing a macro in Word VBA and yet you also seem to be trying to get data from an excel worksheet without properly opening the Excel Workbook

That's what I get from

Code:
'searches for row with "avg" then selects column E(avg of temperature mean) of that row.
    i = Application.Match("Avg", Sheet1.Range("A1:A20"), 0)

This code

Code:
Dim myfile, wdApp As New Word.Application,
Declares myFile as a VARIANT variable and wdApp as a as Word.Applocation object. Is that what you intended.

In the line

Code:
myfile = Application.GetOpenFilename(, , "Browse for Document")
I don't know where 'GetOpenFilename' comes from as its not a Method of the Word.Application object.

Have a look at this

Code:
Sub CopyAndPaste()

Dim my_file                                     As String
Dim my_average                              As Long
Dim my_cell_index                           As Long
Dim my_Doc                                    As Word.Document

    my_file = get_filename
    
    my_average = get_excel_average(my_file, "A1:A20")
    
    ' The excel bit may be aa red herring
    ' Now open the word document.  We can use the DOcuments method as we are already in word.
    ' Do you really wanto to open an excel file as w Word document?
    
        'makes the file appear
   ' wdApp.Visible = True
   ' Set wDoc = wdApp.Documents.Open(myfile)

    Set my_Doc = Documents.Open("filename", True)
    'Dim myfile, wdApp As New Word.Application, wDoc As Word.Document
    
    my_Cell_index = find_last_empty_cell_in_range(my_Doc.Tables(8).Range)
    
    ' put the average into the first empty cell we find in Table 8
    my_Doc.Tables(8).Range.Cells(my_Cell_index).Range.Text = CStr(my_average)
    
End Sub


Public Function get_filename() As String

Dim my_dialog                           As Office.FileDialog

    'select truck report file
'    ChDrive "E:\"
'    ChDir "E:\WG\TVAL\"
   ' myfile = Application.GetOpenFilename(, , "Browse for Document")

    Set my_dialog = Application.FileDialog(msoFileDialogFilePicker)
    my_dialog.Filters.Clear
    If my_dialog.Show Then
        get_filename = my_dialog.SelectedItems(1)
    Else
        Exit Function
    End If

End Function

Public Function get_excel_average(this_filename As String, this_cell_range As String) As Long
' Its not clear what you want to do here
' so this code is an educated guess
' We are using the Create object approach because its unlikey you understand enough
' to have added the reference to excel to allow ' Dim my_XLApp as Excel.APplication'

Dim my_XLApp                             As Object
Dim my_XLWorkbook                        As Object
Dim my_XLWorksheet                       As Object

    Set my_XLApp = CreateObject("Excel.Application")
    my_XLApp.Visible = True
    Set my_XLWorkbook = my_XLApp.Workbooks.Open(this_filename)
    Set my_XLWorksheet = my_XLWorkbook.worksheets(1)
    
            ' Dim i As Integer
   'searches for row with "avg" then selects column E(avg of temperature mean) of that row.
   ' i = Application.Match("Avg", Sheet1.Range("A1:A20"), 0)

    
    ' I suspect that what you wqant is more complicated than this
    ' as it just gets the average of the cells in A1 to A20
    get_excel_average = my_XLWorksheet.worksheetfunction.average(this_cell_range)
    

End Function

Public Function find_last_empty_cell_in_range(this_range As Word.Range) As Long
' Searches through the cells in the table and returns the index of the first empty cell.
' The input parameter is a word range so the function is generic for a table
' or a selection within a table
'
' This function will work with non uniform tables
' Scanning a table using the range.cell(x,y) method will fail with non-uniforma tables
'
' We test if the length of the text in the cell is 2 or less characters to find an empty cell.
' This is because Cells contain hidden characters which mark the end of the cell
' and end of the text (I think)

Dim my_cell                                         As Word.Cell
Dim my_index                                        As Long

    For my_index = 1 To this_range.Cells.Count
        With this_range
            If Len(.Cells(my_index).Range.Text) <= 2 Then
                find_last_empty_cell_in_range = my_index
                Exit Function
            End If
        End With
    Next
End Function
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
extract time from a cell and move it to front of cell before text ewso Excel 20 03-19-2017 11:34 AM
Move the data at bottom cell to right side of the cell kcyag91 Excel 1 01-28-2016 12:28 AM
Data validation,force cell to be filed with number if respective cell is not empty nicholes Excel Programming 0 08-01-2015 09:08 AM
Find last non empty cell (moving horizontally), then move one cell to the right If id cell range is empty then should not allow to fill any other cell ubns Excel Programming 2 04-12-2015 06:31 AM
Move data from 1 cell to another cell Catalin.B Excel 1 06-25-2011 12:51 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:04 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft