热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

根据前四列确定空行-Determineemptyrowbasedonfirstfourcolumns

IamusingaUserForminExceltomovecontentfromtextbox1tofirstemptyrowonsheet2.Belowc

I am using a UserForm in Excel to move content from textbox 1 to first empty row on sheet 2. Below command works fine but I would like to consider an empty row only if the first three columns are empty, not all columns (the others columns have some information).

我在Excel中使用UserForm将内容从文本框1移动到工作表2上的第一个空行。下面的命令工作正常,但我想只考虑前三列是空的,而不是所有列(其他列)的空行有一些信息)。

How can I adjust it?

我该怎么调整呢?

Private Sub CommandButton1_Click()
Dim emptyRow As Long

'Make Sheet2 active
With Sheets("Sheet2")

    'Determine emptyRow
    emptyRow = WorksheetFunction.CountA(.Range("A:A")) + 1

    'Transfer information

    .Cells(emptyRow, 1).Value = TextBox1.Value

5 个解决方案

#1


1  

try this:

Private Sub CommandButton1_Click()
    Dim x&, i&, emptyRow&
    emptyRow = 0
    With Sheets("Sheet2")
        For x = 1 To 3
            i = .Cells(Rows.Count, x).End(xlUp).Row
            If emptyRow 

test:

enter image description here

#2


2  

Look from the bottom up.

从下往上看。

emptyRow = application.max(.cells(rows.count, "A").end(xlup).row, _ 
                           .cells(rows.count, "B").end(xlup).row, _
                           .cells(rows.count, "C").end(xlup).row) + 1

#3


0  

Private Sub CommandButton1_Click()
Dim emptyRow As Long, x as Long

'Make Sheet2 active
With Sheets("Sheet2")

    'Determine emptyRow
    x = 0

    Do 

         x = x +1

         emptyRow = WorksheetFunction.CountA(.Range("A" & x & ":C" & x))

    Loop Until emptyRow = 0

    'Transfer information
    .Cells(x, 1).Value = TextBox1.Value

#4


0  

Test the value of those columns for each row until you find the blank one.

测试每行的这些列的值,直到找到空白列。

You can probably do it with a find also but i'm not sure how off the top of my head.

你也可以用一个发现来做,但我不知道我的头脑是多么偏高。

Private Sub CommandButton1_Click()
    Dim ws As Excel.Worksheet
    Set ws = ActiveWorkbook.Sheets("Sheet2")
    Dim emptyRow As Long

    Dim lrow As Long
    lrow = 1

    ws.Activate
    'Loop through the rows
    Do While lrow <= ws.UsedRange.Rows.count

        'Test for an empty row
        If ws.Range("A" & lrow).Value = "" And ws.Range("B" & lrow).Value = "" And ws.Range("C" & lrow).Value = "" Then
            emptyRow = lrow
            Exit Do
        End If

    lrow = lrow + 1
    Loop

    ws.Range("A" & emptyRow).Value = TextBox1.Value

End Sub

#5


0  

Try this:

Private Sub CommandButton1_Click()
Dim emptyRow As Long
Dim row1  As Long, row2 As Long, row3 As Long

'Make Sheet2 active
With Sheets("Sheet2")

'Determine emptyRow
row1 = .Cells(.Rows.Count,1).End(XlUp).Row + 1
row2 = .Cells(.Rows.Count,2).End(XlUp).Row + 1
row3 = .Cells(.Rows.Count,3).End(XlUp).Row + 1

If row1 = row2 And row1 = row3 Then

emptyRow = WorksheetFunction.CountA(.Range("A:A")) + 1

Else

    If row1 >= row2 And row1 >= row3 Then

    emptyRow = row1

    Elseife row2 >= row3 Then

    emptyRow = row2

    Else

    emptyRow = row3

    End If

End If

'Transfer information

.Cells(emptyRow, 1).Value = TextBox1.Value

So what this does, is that it will check if the last row in column A, B and C are the same, and if not, the emptyRow is set on the "largest" last row of columns A, B or C. I hope, that this is what you where looking for. Else ignore this post.

那么它的作用是,它将检查A,B和C列中的最后一行是否相同,如果不是,则在A,B或C列的“最大”最后一行设置emptyRow。我希望,这就是你在寻找的地方。否则忽略这篇文章。


推荐阅读
author-avatar
手机用户2502921201
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有